package com.stylefeng.guns.modular.poemspoetry.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.poemspoetry.model.PoetryAuthor;
import com.stylefeng.guns.modular.poemspoetry.service.IPoetryAuthorService;

/**
 * 词人控制器
 *
 * @author fengshuonan
 * @Date 2018-03-20 15:57:57
 */
@Controller
@RequestMapping("/poetryAuthor")
public class PoetryAuthorController extends BaseController {

    private String PREFIX = "/poemspoetry/poetryAuthor/";

    @Autowired
    private IPoetryAuthorService poetryAuthorService;

    /**
     * 跳转到词人首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "poetryAuthor.html";
    }

    /**
     * 跳转到添加词人
     */
    @RequestMapping("/poetryAuthor_add")
    public String poetryAuthorAdd() {
        return PREFIX + "poetryAuthor_add.html";
    }

    /**
     * 跳转到修改词人
     */
    @RequestMapping("/poetryAuthor_update/{poetryAuthorId}")
    public String poetryAuthorUpdate(@PathVariable Long poetryAuthorId, Model model) {
        PoetryAuthor poetryAuthor = poetryAuthorService.selectById(poetryAuthorId);
        model.addAttribute("item",poetryAuthor);
        LogObjectHolder.me().set(poetryAuthor);
        return PREFIX + "poetryAuthor_edit.html";
    }

    /**
     * 获取词人列表
     */
    /*
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return poetryAuthorService.selectList(null);
    }
    */

     /**
     * 获取词人列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<PoetryAuthor> page = new PageFactory<PoetryAuthor>().defaultPage();
        EntityWrapper<PoetryAuthor> ew = new EntityWrapper<PoetryAuthor>();
         /*if (ToolUtil.isNotEmpty(condition)) {
                ew.eq("condition", condition);
         }*/
        return super.packForBT(poetryAuthorService.selectPage(page,ew));
    }

    /**
     * 新增词人
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PoetryAuthor poetryAuthor) {
        poetryAuthorService.insert(poetryAuthor);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除词人
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long poetryAuthorId) {
        poetryAuthorService.deleteById(poetryAuthorId);
        return SUCCESS_TIP;
    }

    /**
     * 修改词人
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PoetryAuthor poetryAuthor) {
        poetryAuthorService.updateById(poetryAuthor);
        return super.SUCCESS_TIP;
    }

    /**
     * 词人详情
     */
    @RequestMapping(value = "/detail/{poetryAuthorId}")
    @ResponseBody
    public Object detail(@PathVariable("poetryAuthorId") Long poetryAuthorId) {
        return poetryAuthorService.selectById(poetryAuthorId);
    }
}
